package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.constant.IsStatus;
import com.prd.mapper.EntGoalInterviewMapper;
import com.prd.model.EntGoalInterview;
import com.prd.model.LoginUser;
import com.prd.service.EntGoalInterviewService;
import com.prd.service.UserService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EntGoalInterviewServiceImpl extends ServiceImpl<EntGoalInterviewMapper, EntGoalInterview> implements EntGoalInterviewService {

    @Autowired
    private EntGoalInterviewMapper entGoalInterviewMapper;

    @Autowired
    private UserService userService;

    @Override
    public int insertInfo(EntGoalInterview entGoalInterview) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entGoalInterview);
        entGoalInterview.setVerify(IsStatus.TWO.getCode());
        return entGoalInterviewMapper.insert(entGoalInterview);
    }

    @Override
    public int updateInfo(EntGoalInterview entGoalInterview) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entGoalInterview);
        return entGoalInterviewMapper.updateById(entGoalInterview);
    }

    @Override
    public List<EntGoalInterview> listInfo(EntGoalInterview entGoalInterview) {
        LambdaQueryWrapper<EntGoalInterview> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(entGoalInterview);
        List<EntGoalInterview> list = entGoalInterviewMapper.selectList(queryWrapper);
        for (EntGoalInterview interview : list) {
            interview.setInterviewee(userService.listName(Long.parseLong(interview.getInterviewee())).getMerge());
            interview.setPrincipal(userService.listName(Long.parseLong(interview.getPrincipal())).getMerge());
            interview.setParticipation(userService.listName(Long.parseLong(interview.getParticipation())).getMerge());
            interview.setCompere(userService.listName(Long.parseLong(interview.getCompere())).getMerge());
            interview.setNotekeeper(userService.listName(Long.parseLong(interview.getNotekeeper())).getMerge());
        }
        return list;
    }

    @Override
    public EntGoalInterview details(Long id) {
        return entGoalInterviewMapper.selectById(id);
    }
}
